from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.

class Author(models.Model):
    name = models.CharField("作者姓名", max_length=32)
    tel = models.BigIntegerField("电话号码")
    autherDetail = models.OneToOneField("AuthorDetail", to_field="id", on_delete=models.CASCADE, verbose_name="作者详情")

    def __str__(self):
        return self.name

class AuthorDetail(models.Model):
    age = models.IntegerField("年龄")
    addr = models.CharField("地址", max_length=32)
    email = models.EmailField("e-mail")

    def __str__(self):
        return self.email

class Publish(models.Model):
    name = models.CharField("出版社名字", max_length=32)
    tel = models.BigIntegerField("电话号码")
    addr = models.CharField("地址", max_length=32)
    email = models.EmailField("email")

    def __str__(self):
        return self.name

class Book(models.Model):
    title = models.CharField("书名", max_length=32)
    price = models.DecimalField("价格", max_digits=6, decimal_places=2)
    publish_date = models.DateField("出版日期", auto_now_add=False)
    publish = models.ForeignKey("Publish", to_field="id", related_name="books", on_delete=models.CASCADE, verbose_name="出版社")
    authors = models.ManyToManyField("Author", related_name="books", verbose_name="作者")

    def __str__(self):
        return self.title

    def show_authors(self):
        return "|".join([str(i) for i in self.authors.all()])

class UserInfo(AbstractUser):
    username = models.CharField("用户名", max_length=32, unique=True)
    password = models.CharField("密码", max_length=128)
    create_time = models.DateTimeField("创建时间", auto_now_add=True)

    def __str__(self):
        return self.username
















